package cc.smarnet.caihou.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

import cc.smarnet.caihou.R;
import cc.smarnet.caihou.entity.TagInfo;
import cc.smarnet.caihou.phone.bracts.BractsDetailActivity;
import cc.smarnet.caihou.phone.taginfolists.TaginfoListDetailActivity;

/**
* 毛产品信息的adapter
* */
public class TaginfoAdapter extends RecyclerView.Adapter {
    private Context context;
    private List<TagInfo> list;

    public TaginfoAdapter(Context context, List<TagInfo> list){
        this.context=context;
        this.list=list;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.activity_taginfo_list_item,parent,false);
        TaginfoViewHolder bractsViewHolder=new TaginfoViewHolder(view);
        return bractsViewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        TagInfo tagInfo=list.get(position);
        TaginfoViewHolder bractsViewHolder= (TaginfoViewHolder) holder;

        bractsViewHolder.baseName.setText(tagInfo.getSourceBase());
        bractsViewHolder.variety.setText(tagInfo.getVariety());
        bractsViewHolder.sorter.setText(tagInfo.getSorter());
        bractsViewHolder.sortTime.setText(tagInfo.getSortTime());
        bractsViewHolder.packingSpecifications.setText(tagInfo.getPackingSpecifications());
        bractsViewHolder.classes.setText(tagInfo.getClasses());
        bractsViewHolder.state.setText(tagInfo.getState());
        bractsViewHolder.stockOutTime.setText(tagInfo.getStockOutTime());
        bractsViewHolder.stockInTime.setText(tagInfo.getStockInTime());
        bractsViewHolder.stockOutUser.setText(tagInfo.getStockOutUser());
        bractsViewHolder.stockInUser.setText(tagInfo.getStockInUser());

        Date createTime = tagInfo.getCreateTime();
        String createTime2 = null ;
        if(createTime!=null ){
            createTime2 = createTime.toString();
        }
        bractsViewHolder.createTime.setText(createTime2);

    }

    @Override
    public int getItemCount() {
        // list.size()为0的话也必须至少返回1，返回有数值的话才会显示item，因为该方法不会返回0
        System.out.println("list="+list);
        return list.size() > 0 ? list.size() : 1;
    }

    class TaginfoViewHolder extends RecyclerView.ViewHolder{
        TextView baseName;
        TextView greenhouseName;
        TextView variety;
        TextView varietyCode;
        TextView sorter;
        TextView sortTime;
        TextView packingSpecifications;
        TextView classes;
        TextView flowerPoleLength;
        TextView state;
        TextView stockOutTime;
        TextView stockInTime;
        TextView stockOutUser;
        TextView stockInUser;
        TextView createTime;



        public TaginfoViewHolder(@NonNull View itemView) {
            super(itemView);
            baseName=itemView.findViewById(R.id.item_baseName);
            greenhouseName=itemView.findViewById(R.id.bracts_item_greenhouseName);
            variety=itemView.findViewById(R.id.item_vertical);
            sorter=itemView.findViewById(R.id.item_sorter);
            sortTime=itemView.findViewById(R.id.item_sortTime);
            packingSpecifications=itemView.findViewById(R.id.item_packingSpecifications);
            classes=itemView.findViewById(R.id.item_classes);
            state=itemView.findViewById(R.id.item_state);
            stockOutTime=itemView.findViewById(R.id.item_stockOutTime);
            stockInTime=itemView.findViewById(R.id.item_stockInTime);
            stockOutUser=itemView.findViewById(R.id.item_stockOutUser);
            stockInUser=itemView.findViewById(R.id.item_stockInUser);
            createTime = itemView.findViewById(R.id.createTime);

            // item点击事件
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    TagInfo tagInfo=list.get(getAdapterPosition());
                    Intent intent = new Intent(context, TaginfoListDetailActivity.class);
                    intent.putExtra("taginfoDetail", (Serializable) tagInfo);
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK );
                    context.startActivity(intent);
                }
            });
        }
    }
}
